﻿using ZeroBug.EasyFlow.Core;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace ZeroBug.EasyFlow.Communication
{
    [Serializable]
    public class PLCOperator
    {
        private IPlc _readWriteOperator;
        /// <summary>
        /// 初始是否连接
        /// </summary>
        public bool IsInitConnect { get; set; }
        /// <summary>
        /// 品牌
        /// </summary>
        public string Brand { get; set; }
        /// <summary>
        /// 型号
        /// </summary>
        public string Series { get; set; }
        /// <summary>
        /// IP地址
        /// </summary>
        public string IpAddress { get; set; }
        /// <summary>
        /// 端口号
        /// </summary>
        public int Port { get; set; }
        /// <summary>
        /// 站号
        /// </summary>
        public byte StationNo { get; set; }
        [XmlIgnore]
        public bool IsOpen { get; set; }
        public string Name { get; set; }
        public string Module { get; set; }
        public SerializableDictionary<string, string> SendCmd { get; set; }
        public bool IsOpenFlag
        {
            get { return IsOpen; }
        }
        public List<AddressInfo> AddressShortcut { get; set; }
        public PLCOperator()
        {
            AddressShortcut = new List<AddressInfo>();
        }
        public PLCOperator(string brand, string series)
            : this()
        {
            this.Brand = brand;
            this.Series = series;
        }
        public void Connect()
        {
            if(!IsOpen)
                IsOpen = this._readWriteOperator.Connect(IpAddress, Port, StationNo);
        }
        public void Close()
        {
            this._readWriteOperator.Close();
            IsOpen = false;
        }
        public void SetOperator(IPlc plcOperator)
        {
            this._readWriteOperator = plcOperator;
        }
        public bool Read(RegisterType registerType, DataType dataType, int startAddress, int length, out List<string> values, out int errCode)
        {
            values = new List<string>();
            return _readWriteOperator.Read(Series, registerType, dataType, startAddress, length, out values, out errCode);
        }
        public bool Write(RegisterType registerType, DataType dataType, int startAddress, List<string> values, out int errCode)
        {
            return _readWriteOperator.Write(Series, registerType, dataType, startAddress, values, out errCode);
        }

        public object GetConfig()
        {
            PLCConfig config = new PLCConfig();
            config.IP = IpAddress;
            config.Port = Port;
            return config;
        }

        public void SetConfig(object config)
        {
            if (config is PLCConfig)
            {
                IpAddress = (config as PLCConfig).IP;
                Port = (config as PLCConfig).Port;
            }
        }
    }
    public enum RegisterType
    {
        [Description("AM_QX")]
        QX,
        [Description("AM_MW")]
        MW,
        [Description("H5U_D")]
        D,
        [Description("H5U_M")]
        M,
    }
    public enum DataType
    {
        [Description("Bool")]
        Bool,
        [Description("Short")]
        Short,
        [Description("UShort")]
        UShort,
        [Description("Int")]
        Int,
        [Description("UInt")]
        UInt,
        [Description("Long")]
        Long,
        [Description("ULong")]
        ULong,
        [Description("Float")]
        Float,
        [Description("Double")]
        Double,
        [Description("String")]
        String
    }
}
